package data_report

import (
	"context"
	"strconv"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/analysisClient/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type MemberReportLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 会员报表
func NewMemberReportLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MemberReportLogic {
	return &MemberReportLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *MemberReportLogic) MemberReport(req *types.MemberReportRequest) (resp types.MemberReportReponse, err error) {
	resp = types.MemberReportReponse{
		Data:     make([]types.MemberReportData, 0),
		Page:     req.Page,
		PageSize: req.PageSize,
		Total:    0,
	}

	reqq := &rpc.UserAnalysisRequest{
		UserId:    req.UserId,
		Type:      int32(req.Type),
		StartTime: req.StartTime,
		EndTime:   req.EndTime,
		Sort:      req.Sort,
		Page:      req.Page,
		PageSize:  req.PageSize,
	}

	analysis, err := l.svcCtx.Analysis.UserAnalysis(l.ctx, reqq)
	if err != nil {
		return resp, err
	}

	for _, v := range analysis.List {
		profitAmount, _ := strconv.ParseFloat(v.ProfitAmount, 64)
		t := types.MemberReportData{
			UserId:             v.UserID,
			Balance:            v.Balance,
			ChargeAmount:       v.RechargeAmount,
			ChargeTimes:        v.RechargeCount,
			WithdrawAmount:     v.WithdrawAmount,
			WithdrawTimes:      v.WithdrawCount,
			SafeBoxTransferIn:  v.SafeIncomeAmount,
			SafeBoxTransferOut: v.SafeOutAmount,
			SafeBoxEarings:     v.SafeEarningsAmount,
			OrderAmount:        v.BetAmount,
			RealOrderAmount:    v.RealBetAmount,
			Fee:                v.BetServiceChargeAmount,
			EaringsAmount:      profitAmount,
			Win:                v.WinCount,
			Lose:               v.LoseCount,
			WinRate:            float64(v.WinRate),
		}
		resp.Data = append(resp.Data, t)
	}
	resp.Total = analysis.Total
	return
}
